<template>
  <div class="goods-image">
    <!-- 大图展示区 -->
    <div class="middle" ref="target">
      <img :src= "imageList[curIndex]" alt="">
      <!-- 蒙层容器 -->
      <div v-if="showFlag" class="layer" :style="{ top: top + 'px', left: left + 'px' }">
        {{ elementX }}
        {{elementY}}
        {{isOutside}}
      </div>
    </div>
    <!-- 放大 图片展示 backgroundPosition: (-left * 2 + 'px', -top * 2 + 'px')  -->
    <div v-if="showFlag" class="large"
     :style="{
      backgroundImage:`url(${imageList[curIndex]})`,
      backgroundPositionX: -left * 2 + 'px',
      backgroundPositionY: -top * 2 + 'px'
      }"></div>

    <!-- 小图区 -->
    <ul class="small">
      <li v-for="(img, i) in imageList" :key="i"
      @mouseenter="mouseEnter(i)"
      :class="{ active: i === curIndex }"
      >
        <img :src="img" alt="">
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    imageList: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    // 交互思路分析：
    // 1. 基于鼠标移入事件 mouseenter
    // 2. 鼠标移入哪个小图 就把哪个小图的下标值记录一下，然后通过下标值去imageList中去取值，把渠道的值放到src渲染即可
    const curIndex = ref(0)
    function mouseEnter (i) {
      curIndex.value = i
    }

    const target = ref(null)

    const { elementX, elementY, isOutside } = useMouseInElement(target)

    const left = ref(0)
    const top = ref(0)
    const showFlag = ref(false)
    watch(
      [elementX, elementY, isOutside],
      () => {
        showFlag.value = !isOutside.value

        // 横向
        if (elementX.value < 100) {
          left.value = 0
        }
        if (elementX.value > 300) {
          left.value = 200
        }
        // 纵向
        if (elementY.value < 100) {
          top.value = 0
        }
        if (elementY.value > 300) {
          top.value = 200
        }
        // 可移动区间
        if (elementX.value > 100 && elementX.value < 300) {
          left.value = elementX.value - 100
        }
        if (elementY.value > 100 && elementY.value < 300) {
          top.value = elementY.value - 100
        }
      }
    )

    return { mouseEnter, curIndex, target, elementX, elementY, isOutside, top, left, showFlag }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,&.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}

// 放大展示图
 .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 999;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  // 蒙层
  .layer {
  width: 200px;
  height: 200px;
  background: rgba(0, 0, 0, 0.2);
  left: 0;
  top: 0;
  position: absolute;
}
</style>
